package logic

import (
	"beike/models"
	"context"

	"beike/houserpc/houserpc"
	"beike/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AskListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAskListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AskListLogic {
	return &AskListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AskListLogic) AskList(in *houserpc.AskListRequest) (*houserpc.AskListResponse, error) {
	// todo: add your logic here and delete this line

	var asks []models.Ask
	models.DB.Debug().Order("created_at desc").Find(&asks)

	var askList []*houserpc.AskList

	for _, ask := range asks {
		list := houserpc.AskList{
			UserId:  ask.UserId,
			HouseId: ask.HouseId,
			Content: ask.Content,
		}
		askList = append(askList, &list)
	}

	return &houserpc.AskListResponse{
		List: askList,
	}, nil
}
